<?php

namespace app\modules\backend\controllers;

use app\models\Base;
use app\models\FormType;
use app\models\User;
use app\models\Task;
use app\models\OrderInfo;
use app\models\FormInfo;
use app\tools\Re;
use Yii;
use yii\helpers\Json;

class HomeController extends BaseController
{
    public function actionFormTypeList(){
      $form_type_list = FormType::find()->where(['status' => 1, 'parent_id' => 0])->all();
      $form_type_data = [];
      if ($form_type_list) {
        foreach ($form_type_list as $v) {
          $form_type_data[] = [
            'code' => $v->code,
            'name' => $v->name,
            'icon' => $v->icon,
            'type' => 1
          ];
        }
      }
      $qx_info = [
        'code' => '',
        'name' => '故障抢修',
        'icon' => 'https://qiniu.mocomedia.cn/FnP0CnowWAqhfwvOrmkIx8s4Ygnt.png',
        'type' => 2
      ];
      array_push($form_type_data,$qx_info);
      return Re::data(['data' => $form_type_data]);
    }

    public function actionHomeData(){
        //今日新增用户
        //今日新增需求订单
        //今日新增抢修订单
        //今日新增租赁订单
        $today_start_time = strtotime(date("Y-m-d"),time());
        $today_end_time = $today_start_time + 86400;
        $yesterday_start_time = $today_start_time - 86400;
        $user_count_today = (int)User::find()->where(['user_type' => 1])->andWhere("created_at >= '$today_start_time' and created_at < '$today_end_time'")->count();
        $user_count_yesterday = (int)User::find()->where(['user_type' => 1])->andWhere("created_at >= '$yesterday_start_time' and created_at < '$today_start_time'")->count();
        if ($user_count_yesterday) {
          $user_add_pre = abs($user_count_today - $user_count_yesterday) / $user_count_yesterday * 100 . '%';
        } else {
          $user_add_pre = $user_count_today * 100 . '%';
        }

        $form_count_today = (int)FormInfo::find()->Where("created_at >= '$today_start_time' and created_at < '$today_end_time'")->count();
        $form_count_yesterday = (int)FormInfo::find()->Where("created_at >= '$yesterday_start_time' and created_at < '$today_start_time'")->count();
        if ($form_count_yesterday) {
          $form_add_pre = abs($form_count_today - $form_count_yesterday) / $form_count_yesterday * 100 . '%';
        } else {
          $form_add_pre = $form_count_yesterday * 100 . '%';
        }

        $task_count_today = (int)Task::find()->Where("created_at >= '$today_start_time' and created_at < '$today_end_time'")->count();
        $task_count_yesterday = (int)Task::find()->Where("created_at >= '$yesterday_start_time' and created_at < '$today_start_time'")->count();
        if ($task_count_yesterday) {
          $task_add_pre = abs($task_count_today - $task_count_yesterday) / $task_count_yesterday * 100 . '%';
        } else {
          $task_add_pre = $task_count_yesterday * 100 . '%';
        }

        $order_count_today = (int)OrderInfo::find()->Where("created_at >= '$today_start_time' and created_at < '$today_end_time'")->count();
        $order_count_yesterday = (int)OrderInfo::find()->Where("created_at >= '$yesterday_start_time' and created_at < '$today_start_time'")->count();
        if ($order_count_yesterday) {
          $order_add_pre = abs($order_count_today - $order_count_yesterday) / $order_count_yesterday * 100 . '%';
        } else {
          $order_add_pre = $order_count_yesterday * 100 . '%';
        }

        $data = [
          'user_count_today' => $user_count_today,
          'user_add_pre' => $user_add_pre,
          'user_add_pre_type' => $user_count_today - $user_count_yesterday >= 0 ? 1 : 0,
          'form_count_today' => $form_count_today,
          'form_add_pre' => $user_add_pre,
          'form_add_pre_type' => $form_count_today - $form_count_yesterday >= 0 ? 1 : 0,
          'task_count_today' => $task_count_today,
          'task_add_pre' => $task_add_pre,
          'task_add_pre_type' => $task_count_today - $task_count_yesterday >= 0 ? 1 : 0,
          'order_count_today' => $order_count_today,
          'order_add_pre' => $order_add_pre,
          'order_add_pre_type' => $order_count_today - $order_count_yesterday >= 0 ? 1 : 0,
        ];
        return Re::data(['data' => $data]);
    }

    public function actionUserChartsData(){
      $time = Yii::$app->request->post('user_time', 0);
      $date_info = $this->getDay($time);
      $user_data = [];
      foreach ($date_info['time_stmp'] as $v) {
        $end_time = $v + 86400;
        $user_count = (int)User::find()->where(['user_type' => 1])->andWhere("created_at >= '$v' and created_at < '$end_time'")->count();
        array_push($user_data, $user_count);
      }
      $data = [
        'x_data' => $date_info['date_str'],
        'y_data' => $user_data
      ];
      return Re::data(['data' => $data]);
    }

    public function actionOrderChartsData(){
      $time = Yii::$app->request->post('order_time', 0);
      $date_info = $this->getDay($time);
      $order_data = [];
      foreach ($date_info['time_stmp'] as $v) {
        $end_time = $v + 86400;
        $order_count = (int)OrderInfo::find()->Where("created_at >= '$v' and created_at < '$end_time'")->count();
        array_push($order_data, $order_count);
      }
      $data = [
        'x_data' => $date_info['date_str'],
        'y_data' => $order_data
      ];
      return Re::data(['data' => $data]);
    }

    private function getDay($date) {
      if (!$date) {
        $date = time();
      }
      $date = date("Y-m-d", $date);
      $tem = explode('-' , $date);    //切割日期 得到年份和月份
      $year = $tem['0'];
      $month = (int)$tem['1'];
      if (in_array($month, array(1, 3, 5, 7, 8, 10, 12))) {
        $text = 31;
      } elseif( $month == 2 ) {
        if ( $year%400 == 0 || ($year%4 == 0 && $year%100 !== 0) ) {//判断是否是闰年
          // $text = $year.'年的'.$month.'月有29天';
          $text = '29';
        } else {
          // $text = $year.'年的'.$month.'月有28天';
          $text = '28';
        }
      } else {
        // $text = $year.'年的'.$month.'月有30天';
        $text = '30';
      }
      for ($i = 1; $i <= $text ; $i++) {
        $date_str[] = (int)$month."月".$i.'号';
        $time_stmp[] = strtotime($year.'-'.$month."-".$i);
      }
      $data = [
        'date_str' => $date_str,
        'time_stmp' => $time_stmp,
      ];
      return $data;
  }
}
